﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrainingsApp.Model;
using System.Reflection;
using TrainingsApp.WebModel.Debug;

namespace TrainingsApp.WebModel.Route {
    public class RouteManager {


        readonly IUserState m_UserInfoGeter;

        static readonly Dictionary<string, List<RouteItem>> mRoutePool;
        static readonly object mLock = new object();

        public static string EmptyUrl = "error.aspx?code=404";
        public static readonly string _root = string.Empty;

        public static string Root { get { return _root; } }

        private UserContext m_UserInfo {
            get {
                return m_UserInfoGeter.GetUserContext();
            }
        }

        static RouteManager() {
            mRoutePool = new Dictionary<string, List<RouteItem>>(100);
            _root = System.Configuration.ConfigurationManager.AppSettings["Root"];

        }

        public static void Register(string key, RouteItem item) {
            lock (mLock) {
                if (!mRoutePool.ContainsKey(key)) {
                    mRoutePool.Add(key.ToLower(), new List<RouteItem> { item });
                }
                else {
                    mRoutePool[key].Add(item);
                }
            }
        }

        protected RouteManager(IUserState user) {
            this.m_UserInfoGeter = user;
        }


        public static RouteManager GetInstance(IUserState m_userInfo) {

            return new RouteManager(m_userInfo);
        }

        public string Redirct(string url) {
            return url;
        }

        public string Route(string linkName, object query = null) {
            string subQuery = string.Empty;
            if (query != null) {
                var ps = GetProperties(query.GetType());
                foreach (var pItem in ps) {
                    subQuery += pItem.Name + "=" + pItem.GetValue(query, null).ToString() + "&";
                }

                if (!string.IsNullOrEmpty(subQuery)) { subQuery = subQuery.Substring(0, subQuery.Length - 1); }
            }

            string baseUrl = GetUrl(linkName);
            return baseUrl + ((string.IsNullOrEmpty(subQuery) || baseUrl.IndexOf("?") > 0) ? subQuery : "?" + subQuery);
        }


        public string GetUrl(string linkName) {
            List<RouteItem> routes = null;
            RouteItem route = null;
            if (mRoutePool.TryGetValue(linkName, out routes)) {
                route = routes.FirstOrDefault(u => (u.UserType & (int)m_UserInfo.UserType) > 0);
                if (route != null) return route.Url;
                else return EmptyUrl;
            }
            else {
                return EmptyUrl;
            }
        }

        protected IEnumerable<PropertyInfo> GetProperties(Type type) {
            var pArray = type.GetProperties();
            foreach (var item in pArray) {
                yield return item;
            }
        }
    }


    public class RouteItem {
        public string Url { get; set; }

        public int UserType { get; set; }

        public int Sort { get; set; }

    }
}
